from bs4 import BeautifulSoup

import mysql.connector


def parse():
    s = ""
    with open("jd_goods.html") as reader:
        for line in reader.readlines():
            s = s + line
    conn = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        password="123456",
        database="tea_parse"
    )

    cursor = conn.cursor()

    sql = "INSERT INTO item_url(url) VALUES (%s)"

    soup = BeautifulSoup(s, 'html.parser')
    div = soup.find(id="J_goodsList")
    for li in div.find_all("li"):
        imgDiv = li.find("div", {"class": "p-img"})
        if imgDiv is None:
            continue
        href = "https:" + imgDiv.a["href"]
        print(href)
        cursor.execute(sql, [href])
    conn.commit()


if __name__ == '__main__':
    parse()
